package security.Example;

import java.net.InetAddress;
import java.net.UnknownHostException;

import security.Authentication.UserPass.UserPassHandler;
import security.Authentication.UserPass.UserPassRequest;
import security.Authorization.UserRole.UserRole;
import security.IntrusionDetection.BruteforceIntrusion.BruteForceIntrusion;
import security.LoginSystem.Base.LoginSystem;

/**
 * Example application of the multiple denied login attempts QAS
 */
public class ExampleBruteForce {

	/**
	 * Run the example application
	 * @param args no arguments is needed
	 * @throws UnknownHostException if the local host was not found
	 */
	public static void main(String[] args) throws UnknownHostException {
		LoginSystem loginSystem = new LoginSystem();
		InetAddress localHost = InetAddress.getLocalHost();
		UserPassRequest request =
				new UserPassRequest(localHost, "QwertyUser", "Wrong pass");
		
		// Create BruteForceIntrusion that is an implementation of the
		// abstract class IntrusionDetection to handle the intrusion detection
		BruteForceIntrusion bruteForce = new BruteForceIntrusion();
		
		// Add the authentication, authorization and intrusion detection to
		// the system
		loginSystem.setAuthorization(new UserRole());
		
		// The intrusion detection needs to be added before the authentication
		// if the authentication should be protected by the intrusion detection.
		// This is because of the use of the pattern Chain of responsibility and
		// a proxy that makes the intrusion detection act as an 
		// authentication handler before the real authentication handler will be
		// called in the chain.
		loginSystem.addIntrusionDetection(bruteForce);
		loginSystem.addAuthentication(new UserPassHandler());

		// Test that the brute force works
		loginSystem.login(request);
		loginSystem.login(request);
		loginSystem.login(request);
		loginSystem.login(request);
	}
}
